package com.cfets.annualAssessment.util;

import java.util.*;

import com.qcloud.sms.SmsMultiSender;
import com.qcloud.sms.SmsMultiSenderResult;
import com.qcloud.sms.SmsSingleSender;
import com.qcloud.sms.SmsSingleSenderResult;
//import com.qcloud.sms.SmsSingleVoiceSender;
//import com.qcloud.sms.SmsSingleVoiceSenderResult;
//import com.qcloud.sms.SmsVoiceUploader;
//import com.qcloud.sms.SmsVoiceUploaderResult;
import com.qcloud.sms.SmsStatusPuller;
import com.qcloud.sms.SmsStatusPullCallbackResult;
import com.qcloud.sms.SmsStatusPullReplyResult;
import lombok.extern.log4j.Log4j;

import javax.validation.constraints.Null;

import static java.lang.Integer.parseInt;
@Log4j
public class SmsSDKDemo {
	private static int appid = 1400028728;
	private static String appkey = "36e45b521dffc17f0622e3e223dd4464";
    public static boolean SmsMultiUtil(ArrayList<String> phoneNumbers, String tempNo
			, String SMSPara1, String SMSPara2, String SMSPara3, String SMSPara4, String SMSPara5) {
    	try {
    		//请根据实际 appid 和 appkey 进行开发，以下只作为演示 sdk 使用

    		
//    		String phoneNumber1 = "13816223936";
//    		String phoneNumber2 = "13816223936";
//    		String phoneNumber3 = "13816223936";
//			tempNo = "16842";
    		int tmplId = parseInt(tempNo); //年检通过模板id

//    		 //初始化单发
//	    	SmsSingleSender singleSender = new SmsSingleSender(appid, appkey);
//	    	SmsSingleSenderResult singleSenderResult;
//
//	    	 //普通单发
//	    	singleSenderResult = singleSender.send(0, "86", phoneNumber1, "您注册的验证码：1234", "", "");
//	    	System.out.println(singleSenderResult);
//
//	    	 //指定模板单发
//	    	 //假设短信模板 id 为 1，模板内容为：测试短信，{1}，{2}，{3}，上学。
	    	ArrayList<String> params = new ArrayList<>();
//	    	params.add("指定模板单发");
//	    	params.add("深圳");
//	    	params.add("小明");
//	    	singleSenderResult = singleSender.sendWithParam("86", phoneNumber2, tmplId, params, "", "", "");
//	    	System.out.println(singleSenderResult);
	    	
	    	// 初始化群发
	    	SmsMultiSender multiSender = new SmsMultiSender(appid, appkey);
	    	SmsMultiSenderResult multiSenderResult;
	
	    	// 普通群发
	    	// 下面是 3 个假设的号码
//	    	phoneNumbers = new ArrayList<>();
//	    	phoneNumbers.add(phoneNumber1);
//	    	phoneNumbers.add(phoneNumber2);
//	    	phoneNumbers.add(phoneNumber3);
//	    	multiSenderResult = multiSender.send(0, "86", phoneNumbers, "测试短信，普通群发，深圳，小明，上学。", "", "");
//			multiSenderResult = multiSender.send(0, "86", phoneNumbers, Msg , "", "");
//	    	System.out.println(multiSenderResult);


//	    	// 指定模板群发
//	    	// 假设短信模板 id 为 1，模板内容为：测试短信，{1}，{2}，{3}，上学。
	    	params = new ArrayList<>();
			if(!SMSPara1.isEmpty()) {
				params.add(SMSPara1);
			}
			if(!SMSPara2.isEmpty()) {
				params.add(SMSPara2);
			}
			if(!SMSPara3.isEmpty()) {
				params.add(SMSPara3);
			}
			if(!SMSPara4.isEmpty()) {
				params.add(SMSPara4);
			}
			if(!SMSPara5.isEmpty()) {
				params.add(SMSPara5);
			}
	    	multiSenderResult = multiSender.sendWithParam("86", phoneNumbers, tmplId, params, "", "", "");
	    	System.out.println(multiSenderResult);
			if (multiSenderResult.result!=0){
				return false;
			}
//
//    		//拉取短信回执和回复
//    		SmsStatusPuller pullstatus = new SmsStatusPuller(appid,appkey);
//    		SmsStatusPullCallbackResult callback_result = pullstatus.pullCallback(10);
//    		System.out.println(callback_result);
//    		SmsStatusPullReplyResult reply_result = pullstatus.pullReply(10);
//    		System.out.println(reply_result);
    		
    	} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
    }

	public static SmsStatusPullReplyResult replyResult() throws Exception {
		SmsStatusPullReplyResult reply_result = new SmsStatusPullReplyResult();
		try {
		//拉取短信回执和回复
    		SmsStatusPuller pullstatus = new SmsStatusPuller(appid,appkey);
			reply_result = pullstatus.pullReply(50);
    		System.out.println(reply_result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return reply_result;
	}

	public static boolean SMSSenderUtil(Map<String,ArrayList<String>> SMSMap, String tempNo){
		//初始化单发
		try {
			SmsSingleSender singleSender = new SmsSingleSender(appid, appkey);
			SmsSingleSenderResult singleSenderResult;

			int tmplId = parseInt(tempNo); //年检通过模板id
			Vector<ArrayList<String>> SMSVector=new Vector<>();
			int SMScount = 0;
			int i=0;
			for (Map.Entry<String,ArrayList<String>> entry : SMSMap.entrySet()) {
				if(Objects.equals(entry.getKey(), "phone")){
					if (i!=0){
						log.error("There is no phone num!");
						return false;
					}
					SMSVector.add(entry.getValue());
					SMScount=entry.getValue().size();
					i++;
					continue;
				}
				SMSVector.add(entry.getValue());
				i++;
			}
			for (int j=0;j<SMScount;j++){
				String phonenum="";
				ArrayList<String> params = new ArrayList<>();
				for(int z=0;z<SMSVector.size();z++){
					if (z==0){
						phonenum=SMSVector.get(z).get(j);
						continue;
					}
					if (SMSVector.get(z).get(j).isEmpty()||SMSVector.get(z).get(j)==null){
						params.add(SMSVector.get(z).get(0));
					}else {
						params.add(SMSVector.get(z).get(j));
					}
				}
				log.debug(phonenum+" "+params.get(0));
				singleSenderResult = singleSender.sendWithParam("86", phonenum, tmplId, params, "", "", "");
	    		System.out.println(singleSenderResult);
			}

			} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}
}
